package gob.osinerg.emergencia.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ControlTagHibernateUtil implements Tag{

	private Log log = LogFactory.getLog(ControlTagHibernateUtil.class);
	
	private String DEPARTAMENTO="DEPARTAMENTO_OPCION";
	private String PROVINCIA="PROVINCIA_OPCION";
	private String DISTRITO="DISTRITO_OPCION";
	
	private PageContext pageContext;

	private Object opcion;
	
	private Tag parent;
	
	private String REQUERIDO_SI = "SI";
	
	public int doEndTag() throws JspException {
		return EVAL_PAGE;
	}

	public int doStartTag() throws JspException {
		JspWriter out=pageContext.getOut();
		
		String className = StringUtils.EMPTY;
		try {
			className = this.getClass().getPackage().getName() + "." + BeanUtils.getProperty(opcion, "opcion.tipoOpcion.tag");

		} catch (Exception e) {
			log.error(e);
		}
		System.out.println("Nombre de la Clase :"+className);
		if(StringUtils.isNotEmpty(className)){
			try {
				
				Object obj =  Class.forName(className).newInstance();
				BeanUtils.copyProperty(obj, "maxLength", BeanUtils.getProperty(opcion, "maxlength"));
				BeanUtils.copyProperty(obj, "name", BeanUtils.getProperty(opcion, "opcion.nmbre")+"X");
				BeanUtils.copyProperty(obj, "rows", BeanUtils.getProperty(opcion, "heigth"));
				BeanUtils.copyProperty(obj, "cols", BeanUtils.getProperty(opcion, "width"));
				BeanUtils.copyProperty(obj, "size", BeanUtils.getProperty(opcion, "width"));
				BeanUtils.copyProperty(obj, "label", BeanUtils.getProperty(opcion, "dscrpcion"));
				
				
					
					
					if (obj instanceof RadioControl || obj instanceof CheckBoxControl){
					
					}else if(obj instanceof ComboBoxControl){
						BeanUtils.copyProperty(obj, "label", BeanUtils.getProperty(opcion, "opcion.dscrpcion"));

					}else{
						
					}
				out.println(obj.toString());
			} catch (Exception e) {
				log.error(e);
			}
		}
		return EVAL_BODY_INCLUDE;
	}

	public Tag getParent() {
		return parent;
	}

	public void release() {
		this.pageContext = null;
		this.opcion = null;
		this.log = null;
	}

	public void setPageContext(PageContext pageContext) {
		this.pageContext = pageContext;
		
	}

	public void setParent(Tag parent) {
		this.parent = parent;
	}

	public Object getOpcion() {
		return opcion;
	}

	public void setOpcion(Object opcion) {
		this.opcion = opcion;
	}


	

}